<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="物料代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialCode">
              <HfMaterialSearchSelectTag
                v-model="model.materialCode"
                @change="onMaterialChange(arguments)"
              ></HfMaterialSearchSelectTag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
              <a-input v-model="model.materialName" placeholder="请输入物料名称" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sap">
              <a-input v-model="model.sap" placeholder="请输入规格型号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <j-dict-select-tag
                v-model="model.unit"
                dict-code="hf_material_unit,material_unit_name,id"
                placeholder="请输入单位"
                disabled
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单箱数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perNumber">
              <a-input v-model="model.perNumber" placeholder="请输入单箱数" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialType">
              <a-input
                v-model="model.materialType"
                dict-code="hf_material_type,material_type_name,id"
                placeholder="请输入物料类型"
                disabled
              ></a-input>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="12">-->
          <!--            <a-form-model-item label="返工数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reworkCounts">-->
          <!--              <a-input-number v-model="model.reworkCounts" placeholder="请输入返工数量" style="width: 100%"/>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction, postAction } from '@/api/manage'
import { objectMerge, validateDuplicateValue } from '@/utils/util'
import HfMaterialSearchSelectTag from '@comp/HfManual/HfMaterialSearchSelectTag.vue'

export default {
  name: 'HfReworkDetailModal',
  components: { HfMaterialSearchSelectTag },
  props: ['mainId', 'materialCodes'],
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        materialCode: [
          {
            trigger: 'change',
            validator: (rule, value, callback) => {
              if (this.materialCodes.includes(this.model.materialCode)) {
                callback(new Error('物料代码重复!'))
              } else {
                callback()
              }
            }
          }
        ]
      },
      url: {
        add: '/production/hfRework/addHfReworkDetail',
        edit: '/production/hfRework/editHfReworkDetail'
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          this.model['sourceId'] = this.mainId
          postAction(this.url.add, this.model)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    onMaterialChange([, data]) {
      // 初始化数据
      this.model = JSON.parse(JSON.stringify(data))
    }
  }
}
</script>
